package com.example.myapplication.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore;

import com.example.myapplication.vo.Mp3Info;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

public class MediaUtils {

    private static final Uri albunArtUri = Uri.parse("content:/media/external/audio/albumart");

    /**
     * 根据歌曲id查询到歌曲信息
     */

    public static Mp3Info getNp3Info(Context context , long _id){
        System.out.println(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);

        Cursor cursor  = context.getContentResolver().query(
                MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,null,
                MediaStore.Audio.Media._ID + "=" + _id,null,
                MediaStore.Audio.Media.DEFAULT_SORT_ORDER);

        Mp3Info mp3Info = null;

        if (cursor.moveToNext()){
            mp3Info = new Mp3Info();
             @SuppressLint("Range") long id = cursor.getLong(cursor.
                    getColumnIndex(MediaStore.Audio.Media._ID));//音乐id
             @SuppressLint("Range") String title = cursor.getString(cursor.
                    getColumnIndex(MediaStore.Audio.Media.TITLE));//音乐标题
             @SuppressLint("Range") String artist = cursor.getString(cursor.
                    getColumnIndex(MediaStore.Audio.Media.ARTIST));//歌手
             @SuppressLint("Range") String album = cursor.getString(cursor.
                    getColumnIndex(MediaStore.Audio.Media.ALBUM));//专辑
             @SuppressLint("Range") Long albumId = cursor.getLong(cursor.
                    getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));//
             @SuppressLint("Range") Long duration = cursor.getLong(cursor.
                    getColumnIndex(MediaStore.Audio.Media.DURATION));//时常
             @SuppressLint("Range") long size = cursor.getLong(cursor.
                    getColumnIndex(MediaStore.Audio.Media.SIZE));//文件大小
             @SuppressLint("Range") String url = cursor.getString(cursor.
                    getColumnIndex(MediaStore.Audio.Media.DATA));//文件路径
             @SuppressLint("Range") int isMusic = cursor.getInt(cursor.
                    getColumnIndex(MediaStore.Audio.Media.IS_MUSIC));//是否为音乐
            if (isMusic != 0){//如果判定是音乐，就把该音乐添加到集合中
                mp3Info.setId(id);
                mp3Info.setTitle(title);
                mp3Info.setArtist(artist);
                mp3Info.setAlbum(album);
                mp3Info.setAlbumId(albumId);
                mp3Info.setDuration(duration);
                mp3Info.setSize(size);
                mp3Info.setUrl(url);
            }

        }
        cursor.close();//关闭
        return mp3Info;
    }

    /**
     *
     * @param context
     * @return
     */
    public static Long[] getMp3InfoIds(Context context){
        Cursor cursor = context.getContentResolver().query(
                MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                new String[]{MediaStore.Audio.Media._ID},
                MediaStore.Audio.Media.DURATION + ">=180000",null,
                MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
        Long[] ids = null;
        if (cursor != null){
            ids = new Long[cursor.getCount()];
            for (int i = 0;i < cursor.getCount();i ++){
                cursor.moveToNext();
                ids[i] = cursor.getLong(0);
            }
        }
        cursor.close();
        return ids;
    }

    /**
     * 用于从数据库中查询歌曲的信息，保存在List当中
     */
    public static ArrayList<Mp3Info> getMp3Infos(Context context){
        Cursor cursor = context.getContentResolver().query(
                MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null,
                //最小音乐长度
                MediaStore.Audio.Media.DURATION + ">=180000", null,
                MediaStore.Audio.Media.DEFAULT_SORT_ORDER);

        ArrayList<Mp3Info> mp3Infos = new ArrayList<Mp3Info>();
        for(int i=0;i<cursor.getCount();i++){
            cursor.moveToNext();
            Mp3Info mp3Info = new Mp3Info();
            @SuppressLint("Range") long id = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media._ID));//id
            @SuppressLint("Range") String title = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));//歌名
            @SuppressLint("Range") String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));//艺术家
            @SuppressLint("Range") String album = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));//专辑
            @SuppressLint("Range") long albumId = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));//专辑id
            @SuppressLint("Range") long duration = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION));//时长
            @SuppressLint("Range") long size = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.SIZE));//大小
            @SuppressLint("Range") String url = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));//路径
            @SuppressLint("Range") int isMusic = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.IS_MUSIC));//是否为音乐

            if(isMusic!=0){
                mp3Info.setId(id);
                mp3Info.setTitle(title);
                mp3Info.setArtist(artist);
                mp3Info.setAlbum(album);
                mp3Info.setAlbumId(albumId);
                mp3Info.setDuration(duration);
                mp3Info.setSize(size);
                mp3Info.setUrl(url);
                mp3Infos.add(mp3Info);
            }
        }

        cursor.close();
        return mp3Infos;
    }

    /**
     * 往list集合中添加Map数据，每一个map对象存放一首音乐的所有属性
     */
    public static List<HashMap<String,String>> getMusicMaps(List<Mp3Info> mp3Infos){
        List<HashMap<String,String>> mp3List = new ArrayList<>();

        for (Iterator iterator = mp3Infos.iterator();iterator.hasNext();){
            Mp3Info mp3Info = (Mp3Info) iterator.next();
            HashMap<String,String> map = new HashMap<>();

            map.put("title",mp3Info.getTitle());
            map.put("Artist",mp3Info.getArtist());
            map.put("album",mp3Info.getAlbum());
            map.put("albumId",String.valueOf(mp3Info.getAlbumId()));
            map.put("duration",formatTime(mp3Info.getDuration()));
            map.put("size",String.valueOf(mp3Info.getSize()));
            map.put("url",mp3Info.getUrl());
            mp3List.add(map);
        }
        return mp3List;
    }
    /**
     * 格式化时间，将毫秒转化为分：秒格式
     */
    public static String formatTime(Long time){
        String min = time/(1000*60) + "";
        String sec = time % (1000*60) + "";
        if (min.length() < 2){
            min = "0"+time/(1000*60)+"";
        }else {
            min = time /(1000*60) + "";
        }
        if (sec.length() == 4){
            sec = "0" + (time%(1000*60)) + "";
        }else if (sec.length() == 3){
            sec = "00" + (time%(1000*60)) + "";
        }else if (sec.length() == 2){
            sec = "000" + (time%(1000*60)) + "";
        }else if (sec.length() == 1){
            sec = "0000" + (time%(1000*60)) + "";
        }
        return min + ":" + sec.trim().substring(0,2);
    }
}
